<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/1/31
 * Time: 上午10:21
 * 微信公众号统计控制器
 */
require_once APPPATH.'third_party/PHPExcel/PHPExcel/IOFactory.php';
class Weixinstat extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    /* 获取用户增减数据
     * 自变量：统计日期
     * 因变量：统计日期当天，分渠道统计用户增减数据
     * 最大时间跨度：7
     * */
    public function getUserSummary($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserSummary($source,$start,$end);
    }
    /* 获取累计用户数据
     * 自变量：统计日期
     * 因变量：统计日期当天，用户数量
     * 最大时间跨度：7
     * */
    public function getUserCumulate($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserCumulate($source,$start,$end);
    }
    /* 获取图文群发总数据
     * 自变量：文章
     * 因变量：群发日期到接口调用日期（最多7天）间，文章阅读分享收藏数据
     * 最大时间跨度：1
     * */
    public function getArticleTotal($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getArticleTotal($source,$start,$end);
    }
    /* 获取图文群发每日数据
     * 自变量：文章
     * 因变量：统计日期当天，文章的阅读分享收藏数据
     * 最大时间跨度：1
     * */
    public function getArticleSummary($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getArticleSummary($source,$start,$end);
    }
    /* 获取图文统计数据
     * 自变量：统计日期
     * 因变量：统计日期当天，聚合统计所有文章的阅读分享收藏数据
     * 最大时间跨度：3
     * */
    public function getUserRead($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserRead($source,$start,$end);
    }
    /* 获取图文统计分时数据
     * 自变量：统计日期的时刻
     * 因变量：统计日期的某个时刻，分渠道聚合统计所有文章的阅读分享收藏数据
     * 最大时间跨度：1
     * */
    public function getUserReadHour($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserReadHour($source,$start,$end);
    }
    /* 获取图文分享转发数据
     * 自变量：统计日期
     * 因变量：统计日期当天，分渠道聚合统计所有文章的分享数据
     * 最大时间跨度：7
     * */
    public function getUserShare($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserShare($source,$start,$end);
    }
    /* 获取图文分享转发分时数据
     * 自变量：统计日期的时刻
     * 因变量：统计日期的某个时刻，分渠道聚合统计所有文章的分享数据
     * 最大时间跨度：1
     * */
    public function getUserShareHour($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $this->weixin_stat_model->getUserShareHour($source,$start,$end);
    }

    public function createArticleReadWeekExcel($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $data = $this->weixin_stat_model->getArticleReadWeekData($source,$start,$end);
        $obj = new PHPExcel();
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->fromArray($data['total'],'','A1');
        $obj->createSheet();
        $obj->setActiveSheetIndex(1);
        $obj->getActiveSheet()->fromArray($data['summary'],'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("weixin-article-read-week-data-".$source.".xlsx");
        echo "weixin-article-read-week-data-".$source.".xlsx comlpete".PHP_EOL;

    }

    public function createUserWeekExcel($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2017-01-01 00:00:00";
        $end = "2018-01-01 00:00:00";
        $data = $this->weixin_stat_model->getUserWeekData($source,$start,$end);
        $obj = new PHPExcel();
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->fromArray($data['total'],'','A1');
        $obj->createSheet();
        $obj->setActiveSheetIndex(1);
        $obj->getActiveSheet()->fromArray($data['newuser'],'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("weixin-user-week-data-".$source.".xlsx");
        echo "weixin-user-week-data-".$source.".xlsx comlpete".PHP_EOL;
    }

    public function createArticleOpenRExcel($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $data = $this->weixin_stat_model->getArticleOpenR($source,$start,$end);
        $obj = new PHPExcel();
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("weixin-article-open-ratio-".$source.".xlsx");
        echo "weixin-article-open-ratio-".$source.".xlsx comlpete".PHP_EOL;
    }

    public function createArticleReadHourOfWeekExcel($source='')
    {
        $this->load->model('weixin_stat_model');
        $start = "2018-01-01 00:00:00";
        $end = "2019-01-01 00:00:00";
        $weekdaystart = 7;//$start日期下是星期日
        $data = $this->weixin_stat_model->getArticleReadHourOfWeek($source,$start,$end,$weekdaystart);
        $obj = new PHPExcel();
        $obj->setActiveSheetIndex(0);
        $obj->getActiveSheet()->fromArray($data,'','A1');
        $objw = new PHPExcel_Writer_Excel2007($obj);
        $objw->save("weixin-article-week-hour-read-".$source.".xlsx");
        echo "weixin-article-week-hour-read-".$source.".xlsx comlpete".PHP_EOL;
    }

    //同步文章发送信息
    public function updateArticleDataToDB()
    {
        $this->load->model('weixin_stat_model');
        $this->weixin_stat_model->updateArticleDataToDB();
    }

    public function format()
    {
        $this->load->model('weixin_stat_model');
        $this->weixin_stat_model->format();
    }
}